import axios from 'axios'
import {Message} from 'element-ui'
import qs from 'qs'


//请求拦截器
axios.interceptors.request.use(config => {
    if (window.sessionStorage.getItem("userToken") || window.sessionStorage.getItem("adminToken")) {
        config.headers['userToken'] = window.sessionStorage.getItem("userToken");
        config.headers['adminToken'] = window.sessionStorage.getItem("adminToken");
    }
    config.data = qs.stringify(config.data);
    return config;
})
// 响应拦截器
axios.interceptors.response.use(success => {
// 响应成功200
    if (success.status === 200) {

        if (success.data.code === 401) {
            Message.error({message: success.data.message})
            return
        }
    }
    return success.data
}, error => {
    Message.error({message: '未知错误,请联系管理员!!!'})
})

let base = '';

export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: url,
        data: params,

    })
}

export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: url,
        params: params,

    })
}

export const deleteRequest = (url, params) => {
    return axios({
        method: 'delete',
        url: url,
        params: params,

    })
}

export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: url,
        params: params,

    })
}

//处理图片
export const fun_GetImgCode = (url, params) => {
    return axios({
        url: `${url}`,
        responseType: "arraybuffer", // 类型
        method: "get",
        params
    });
};

export const beforeOperate = () => {
    if (window.sessionStorage.getItem("user") && window.sessionStorage.getItem("userToken") != "")
        return true;
    else
        return false;
}

